package com.example.Controller;


import com.example.exception.MyException;
import com.example.util.FileUploadUtil;
import com.example.util.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Api(tags = "文件管理")
@RestController
@CrossOrigin
public class FileController {
    @Autowired
    private FileUploadUtil fileUploadUtil;

    @PostMapping("upload-img")
    public Result uploadImg(@RequestParam("file")MultipartFile file){
        InputStream inputStream = null;
        try {
            inputStream=file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyException("上传文件异常","606");
        }
        String url=fileUploadUtil.upload(inputStream, file.getOriginalFilename());
        return new Result("200","上传成功",url);
    }

}
